// pages/helpmessage/helpmessage.js
const app = getApp()
const qiniu = require("../../utils/qiniu.min.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    message: '',
    maxLength: 1000,
    currentLength: 0,
    imagesUrl: [],
    uploadedImages: [],
    telephone: '',
    img1: '',
    img2: '',
    img3: '',
    nametxt: '',
  },

  bindInput1(e) {
    this.setData({
      nametxt: e.detail.value
    })
  },
  bindInput(e) {
    this.setData({
      telephone: e.detail.value
    })
  },


  chooseImage: function () {
    var that = this;
    app.getQiNiuToken(function (res) {
      var qiniuconfig = res;
      wx.chooseImage({
        count: 3,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          const filepathArray = res.tempFilePaths
          for (let i = 0; i < filepathArray.length; i++) {
            wx.showLoading({
              title: '图片上传中',
            })
            qiniu.upload(filepathArray[i], res => {
              that.setData({
                imagesUrl: that.data.imagesUrl.concat([res.imageURL])
              })
              console.log("网络图片", that.data.imagesUrl)
              wx.hideLoading()
            }, (error) => {
              wx.hideLoading()
              wx.showToast({
                title: '请求超时，请稍后重试',
                icon: 'none',
                duration: 2000
              })
            }, {
                uploadURL: 'https://upload.qiniup.com',
                region: 'ECN',
                domain: qiniuconfig.qiniuDomain,
                uptoken: qiniuconfig.upToken,
              })
          }

        }
      })
    });
  },

  delete: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    this.remove(that.data.imagesUrl, that.data.imagesUrl[index]);
    that.setData({
      imagesUrl: that.data.imagesUrl
    });
  },

  //删除元素
  remove: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);
      }
    }
  },

  messageInput: function (e) {
    var message = e.detail.value,
      currentLength = e.detail.value.length;
    if (currentLength > this.data.maxLength) {
      wx.showToast({
        title: '输入文字数量已达到上限',
        icon: 'none',
        duration: 2000
      })
      return this.data.message;
    }
    this.setData({
      message: message,
      currentLength: currentLength
    })
  },

  //提交
  saveContent: function () {
    var that = this;
    var data = that.data;
    if (!data.message) {
      wx.showToast({
        title: '请输入内容后提交',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    that.setData({
      img1: '',
      img2: '',
      img3: ''
    })
    for (let i = 0; i < data.imagesUrl.length; i++) {
      if (i == 0) {
        that.setData({
          img1: data.imagesUrl[i]
        })
      } else if (i == 1) {
        that.setData({
          img2: data.imagesUrl[i]
        })
      } else if (i == 2) {
        that.setData({
          img3: data.imagesUrl[i]
        })
      }
    }
    app.req({
      url: "blessmini/user/feedback",
      method: "post",
      data: {
        content: data.message,
        tel: data.telephone,
        uName: data.nametxt,
        img1: data.img1,
        img2: data.img2,
        img3: data.img3
      },
      success: function (res) {
        if (200 == res.code) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
          wx.showToast({
            title: "提交完成",
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },

  phoneTo: function (event) {
    var dataset = event.target.dataset;
    wx.makePhoneCall({
      phoneNumber: dataset.phone // 仅为示例，并非真实的电话号码
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})